package bean;

import java.io.File;
import java.nio.file.Path;
import java.nio.file.Paths;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;

import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
import metier.Personne;
import DAO.*;

public class BeanPersonne implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Personne personne = new Personne();
	private DAOPersonne dao = DAOPersonneJPA.getInstance();
	private int idCourant;
	UploadedFile file;
	
	public int getIdCourant() {
		return idCourant;
	}

	public void setIdCourant(int idCourant) {
		this.idCourant = idCourant;
		this.personne=dao.get(idCourant);
		// on pense � mettre la popularit� a jour
		this.popularite();
	}
	
	public UploadedFile getFile() {
		return file;
	}

	public void setFile(FileUploadEvent file) {
		this.file = file.getFile();

	}

	public void setImage() throws IOException {
		
			InputStream inputStream = null;
			OutputStream outputStream = null;
			File f=new File("img/test.png");
			try {
				// read this file into InputStream
				inputStream = this.file.getInputstream();
		 
				// write the inputStream to a FileOutputStream
			
				Path folder = Paths.get("img/");
				String filename = personne.getId() + "";
				String extension = "png";
				outputStream = 
		                    new FileOutputStream(new File(folder.toString()+filename+'.'+extension));
		 
				int read = 0;
				byte[] bytes = new byte[1024];
		 
				while ((read = inputStream.read(bytes)) != -1) {
					outputStream.write(bytes, 0, read);
				}
		 
				System.out.println("Done!");
		 
			} catch (IOException e) {
				e.printStackTrace();
			}
//			Path folder = Paths.get("img/");
//			String filename = personne.getId() + "";
//			String extension = "png";
//
//			Path file = Files.createTempFile(folder, filename, "."
//					+ extension);
//			InputStream input = this.file.getInputstream();
//			Files.copy(input, file, REPLACE_EXISTING);
//			personne.setImage(folder.toString()+filename+'.'+extension);
		
		
	}

	public String getImage() {
		return personne.getImage();
	}

	public Personne getPersonne() {
		return personne;
	}
	
	public void setPersonne (Personne personne) {
		this.personne = personne;
	}
	
	public String enregistrer() {

		dao.save(personne);
		try {
			setImage();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			System.out.println("ERREUR FILE : ");
			e.printStackTrace();
		}
		personne=new Personne();
		return "PersonneEnregistre";
	}
	
	public List<Personne> getPersonnes(){
		return dao.loadAll();	
	}
	
	public void popularite(){
		personne.incrPopularite();
	}
	
	public String modifier() {
		dao.update(personne);
		personne=new Personne();
		return "PersonneModif";
	}
	

	
}
